#include "Down.h"

//************************************
// Method:    	File_Read					读取文件函数
// Programmer:  lww
// Time:		2018/12/27
// Returns:   	bool						true-成功；false-失败
//************************************
bool File_Read()
{
	remove(TEMP_FILE_PATH);	//< 删除temp.tmp文件
	//< 读取DATA文件
	if (false == Read_DATA_File()) { printf("Read DATA Wrong\r\n"); return false; }
	//< 读取SAMA文件
	if (false == Read_SAMA_File()) { printf("Read SAMA Wrong\r\n"); return false; }
	//< 开始执行读卡程序
	SYS_ReadCard_Start = true;
	return true;
}


//************************************
// Method:    	DownCMD_Deal				下装命令处理
// Programmer:  lww
// Time:		2018/12/26
// Returns:   	void
// Parameter: 	char * Recv					命令字
//************************************
void DownCMD_Deal(char* Recv)
{
	//< 如果当前身份是主站
	if (SYS_Role == Master_Alone_Role || SYS_Role == Master_Twins_Role)
	{
		if (strcmp(Recv, "FDWN0") == 0)	//< 主站初始下装，下装三个文件
		{
			//< 判断文件是否下装完成
			while (!(File_Exist_Check(SAMA_FILE_PATH) && File_Exist_Check(DATA_FILE_PATH) && File_Exist_Check(TEMP_FILE_PATH)));
			File_Read();			//< 读取文件
		}
		else if (strcmp(Recv, "FDWN1") == 0)
		{

		}
		else if (strcmp(Recv, "FDWN2") == 0)
		{

		}
		else if (strcmp(Recv, "FDWN3") == 0)
		{

		}
		else if (strcmp(Recv, "FCOPY") == 0)
		{

		}
		else if (strcmp(Recv, "MSTOP") == 0)
		{

		}
		else if (strcmp(Recv, "FSTOP") == 0)								//< duan 重启主站
		{
			Del_File(SAMA_FILE_PATH);
			Del_File(DATA_FILE_PATH);
			printf("Master_Role-Delete SAMA and Data file success\r\n");
			printf("Master_Role Restart\r\n");
			if (-1 == reboot(RB_AUTOBOOT))
				printf("the Master_Role Restart Fail\r\n");

		}
	}
	if (SYS_Role == Slave_Role)
	{
		if (strcmp(Recv, "FDWNS") == 0)
		{

		}
		else if (strcmp(Recv, "FDWN1") == 0)
		{

		}
		else if (strcmp(Recv, "FDWN2") == 0)
		{

		}
		else if (strcmp(Recv, "FDWN3") == 0)
		{

		}
		else if (strcmp(Recv, "MSTOP") == 0)							//< duan 主从站切换
		{

		}
		else if (strcmp(Recv, "CLEAN") == 0)							//< duan 重启从站
		{
			Del_File(SAMA_FILE_PATH);
			Del_File(DATA_FILE_PATH);
			printf("Slave_Role-Delete SAMA and Data file success\r\n");
			printf("Slave_Role Restart\r\n");
			if (-1 == reboot(RB_AUTOBOOT))
				printf("the Slave_Role Restart Fail\r\n");
		}
	}
}


//************************************
// Method:    	tFirstDown2Slave			从站初始在线下装线程
// Programmer:  lww
// Time:		2019/3/24
// Returns:   	void *
// Parameter: 	void * arg
//************************************
void *tFirstDown2Slave(void* arg)
{
#define FTP_SEND_FILE(name)	{\
	do\
	{\
		if (Lan_Status == 2) { Lan_Status = 0; }\
		if (Lan_Status == 0)\
		{\
			type = FtpPutFile(NETA_NAME, tmp_char, FTP_NAME, FTP_PASSWD, name, name);\
			if (type == false)\
			{\
				type = FtpPutFile(NETB_NAME, tmp_char, FTP_NAME, FTP_PASSWD, name, name);\
				if (type == false) { Lan_Status = 2; continue; }/*两个网段都不可用*/\
				else { Lan_Status = 1; break; }/*1网段可用*/\
			}\
			else { Lan_Status = 0; break; }/*0网段可用*/\
		}\
		if (Lan_Status == 1)\
		{\
			type = FtpPutFile(NETB_NAME, tmp_char, FTP_NAME, FTP_PASSWD, name, name);\
			if (type == false)\
			{\
				type = FtpPutFile(NETA_NAME, tmp_char, FTP_NAME, FTP_PASSWD, name, name);\
				if (type == false) { Lan_Status = 2; continue; }\
				else { Lan_Status = 0; break; }\
			}\
			else { Lan_Status = 1; break; }\
		}\
	} while (Lan_Status != 2);\
}

	bool flag = false;
	char tmp_char;
	int Lan_Status = 0;			//< 网段信息标志，为0表明0网段可用，为1表明1网段可用，为2代表两个网段都不可用
	bool type;

	printf("the Kernel_%s is %d\r\n", "tFirstDown2Slave_ID", syscall(SYS_gettid));			//< duan20200610
	while (1)
	{
		while (SYS_Role != Master_Twins_Role)//系统为主站且从站存在时，才会经FTP传送数据库和SAMA
		{
			flag = false;
			ExactDelay(100);
		}
		if (SYS_Role == Master_Twins_Role && SYS_Status == Run_Status && flag == false)
		{
			ExactDelay(5000);												//< duan 0903 延时12000毫秒，等待从站删除文件成功
			tmp_char = (char)((IPTail_Num > MASTER_SLAVE_ID_DT) ? (IPTail_Num - MASTER_SLAVE_ID_DT) : (IPTail_Num + MASTER_SLAVE_ID_DT));//给对方发
			if (File_Exist_Check(SAMA_FILE_PATH) && File_Exist_Check(DATA_FILE_PATH))		//< duan20200508添加该判断条件
			{
				//由主站的三个路径发到从站同样的三个路径下（FTP发送）
				FTP_SEND_FILE(DATA_FILE_PATH);
				ExactDelay(100);
				FTP_SEND_FILE(SAMA_FILE_PATH);
				ExactDelay(100);
				FTP_SEND_FILE(MASTER_SEND2_SALVE_TEMP_FILE_PATH);
				ExactDelay(100);
				remove(MASTER_SEND2_SALVE_TEMP_FILE_PATH);	//< 删除M2Stemp.tmp文件
				flag = true;
			}
			else
			{
				printf("ERROR-The Master_Twins_Role can't have SAMA and DATA files,please shutdown\r\n");
			}
			
		}
		ExactDelay(100);
	}
}


//************************************
// Method:    	tSlave_RecvFirstDown		从站等待初始下装完成
// Programmer:  lww
// Time:		2019/3/24
// Returns:   	void *
// Parameter: 	void * arg
//************************************
void *tSlave_RecvFirstDown(void* arg)
{
	printf("the Kernel_%s is %d\r\n", "tSlave_RecvFirstDown_ID", syscall(SYS_gettid));			//< duan20200610
	while (1)
	{
		ExactDelay(2000);							//< duan20200405
		//< 判断文件是否下装完成
		if (SYS_Role == Slave_Role) {
			if (File_Exist_Check(SAMA_FILE_PATH) && File_Exist_Check(DATA_FILE_PATH) && File_Exist_Check(MASTER_SEND2_SALVE_TEMP_FILE_PATH))
			{
				remove(MASTER_SEND2_SALVE_TEMP_FILE_PATH);
				//< 切换系统状态为禁止命令下发
				SYS_Status_Turn(Forbid_Commend_Status);
				File_Read();			//< 读取文件
				break;
			}
		}

	}
	return NULL;
}


//************************************
// Method:    	tDown				下装/在线下装
// Programmer:  lww
// Time:		2018/12/26
// Returns:   	void *
// Parameter: 	void * arg
//************************************
void *tDown(void* arg)
{
	int sock, sock_accept;
	char Recv[7] = { 0 };
	pthread_t tFirstDown2Slave_ID = 0;
	pthread_t tSlave_RecvFirstDown_ID = 0;

	printf("the Kernel_%s is %d\r\n", "tDown_ID", syscall(SYS_gettid));			//< duan20200610

	//< 开启从站初始在线下装线程
	THREAD_CREATE_FAILED(Thread_Create("tFirstDown2Slave_ID", &tFirstDown2Slave_ID, NULL, SCHED_POLICY, 89, true, tFirstDown2Slave, CONTROL_CORE, NULL));
	//从站等待初始下装完成并读取文件
	THREAD_CREATE_FAILED(Thread_Create("tSlave_RecvFirstDown_ID", &tSlave_RecvFirstDown_ID, NULL, SCHED_POLICY, 89, true, tSlave_RecvFirstDown, CONTROL_CORE, NULL));
	
	//< 初始化TCP服务器端设置
	if (false == TCP_Sever_Init(&sock, DOWN_CMD_PORT, 1, nullptr))
		return 0;
	while (1)
	{
		//< 等待被连接
		if (false == TCP_Sever_Accept(&sock, &sock_accept, 5))
			continue;
	
		//< 接收数据
		memset(Recv, 0, sizeof(Recv));
		if (-1 == TCP_Recv(&sock_accept, Recv, 5, 10000))
			continue;

		printf("Recv = %s\r\n", Recv);

		//< 断开连接
		close(sock_accept);

		//< 切换系统状态为禁止命令下发
		SYS_Status_Turn(Forbid_Commend_Status);

		RoughDelay_S(5);	//< 等待文件传输完成
		//< 指令处理
		DownCMD_Deal(Recv);
	}
	close(sock);
}
